<?php
/**
Template Name: Business Listing Category
*/

get_header(); ?>

		<div id="primary" class="pagedefault business-partner">
			<div id="content" role="main" class="clearfix">
                <div class="leftpage aleft">  
                	<?php

                		if(has_post_thumbnail()) {the_post_thumbnail('page-thumb');
                		} else { ?>
                				<img src="<?php echo get_template_directory_uri(); ?>/images/page-thumb.jpg" alt="<?php the_title();?>"/>
                	<?php } //endif?>
            		<?php 
            			$args = array(
            					'menu'		        => get_field('menu_name'), 
            					'container'       	=> false, 
            					'menu_class'		=> 'pagemenu' );
            			echo wp_nav_menu($args); 
            		?>
                    <div id="attendees-forms-left">
                        <img src="<?php echo get_template_directory_uri(); ?>/images/events_search_table.gif" />
                        <div class="eventscalendar">
                            <p>We're a busy place but never too busy to help you <a href="/eventscalendar/">find an event</a>. 
                        </div>
                        <div class="buy-attraction-online">
                            <script src="http://arestravel.com/js/_default/ARESbot.js?bundle=sd_conventioncenter&irt=1" type="text/javascript"></script>
                        </div>
                    </div>
                </div><!--/.leftpage-->              

                <div class="centerpage aleft busines_category">
                    <?php
                        //get category info                        
                        $term = get_term(absint($_GET['catid']), 'bs_category');

                        $class = str_replace(' ','',$term->name);     
                        $class = str_replace('&amp;','-',$class);       
                        $sql = "select category_id, field_key, field_label, field_type from wp_category_fields a
                                inner join wp_content_fields b on b.id = a.field_id
                                where search = 1 and category_id=".$_GET['catid']." order by b.weight desc";
                                
                        $db = $wpdb->get_results($sql);
                    ?>
                    <div id="category-title" class="<?php print strtolower($class);?>">
                        <h2><?php print $term->name; ?></h2>
                    </div>
                    <div id="search_description">
                        <div id="search">
                            <div class="content-search">
                                <div class="body">
                                   <strong>Search by:</strong> 
                                   <form action="" method="get" id="frmSearch" name="frmSearch">
                                    <?php
                                        foreach($db as $field ) {
                                            if($field->field_type=='textfield') {
                                                print '<input rel="'.$field->field_label.'" size="15" type="text" value="'.$field->field_label.' & Keyword" id="'.$field->field_key.'" id="'.$field->field_key.'">
                                                        <input type="image" src="'.get_template_directory_uri().'/images/searchBtn.jpg" class="img-button">';
                                            } else {
                                                print '<div class="select"><div id="text_'.$field->field_key.'" class="text">--'.$field->field_label.'--</div>';
                                                print select_control($field->field_key,$field->field_type,$_GET['catid'], $val, false, 'styled', $field->field_label);
                                                print '</div>';
                                            }
                                        }
                                    ?>
                                   </form> 
                                </div>
                            </div>
                        </div>
                        <div id="description"><?php print $term->description; ?></div>
                    </div>
                    <?php
                        switch ( $_GET['catid']){
                            case 35:
                                $key_google = 'Featured-Arts-Page-TOP';
                            break;
                            case 40:
                                $key_google = 'Featured-Attractions-Page-TOP';
                            break;
                            case 37:
                                $key_google = 'Featured-Hotels-Page-TOP';
                            break;
                            case 36:
                                $key_google = 'Featured-Nightlife-Page-TOP';
                            break;
                            case 38:
                                $key_google = 'Featured-Restaurant-Page-TOP';
                            break;
                            case 100:
                                $key_google = 'Featured-ServicesDirectory-Page-TOP';
                            break;
                            case 39:
                                $key_google = 'Featured-Shopping-Page-TOP';
                            break;
                            case 101:
                                $key_google = 'Featured-SpecialEventVenues-Page-TOP';
                            break;
                        }
                        
                        //Featured-Restaurant-Page-TOP
                    ?>
                    <!-- BEGIN: GOOGLE BANNERS: PUT THIS TAG IN THE head SECTION -->
            		<script type="text/javascript">			
                           	GA_googleFillSlot("<?php print $key_google;?>");            
            		</script>
            		<!-- END: GOOGLE BANNERS: END OF TAG FOR head SECTION -->
                    <div id="header-pager">
                        Sort by: <select id="sort_by" name="sort_by">
                                    <option value="">--Type--</option>
                                    <option value="">Name</option>
                                </select>
                    </div>
                    <?php                        
                        $paged = (get_query_var('paged')) ? absint(get_query_var('paged')) : 1;
                        $per_page = 20;
                        $sql = "select p.ID, p.post_content, p.post_title from wp_posts p
                                inner join wp_postmeta pm on pm.post_id = p.ID
                                inner join wp_term_relationships tr on tr.object_id = p.ID
                                inner join wp_term_taxonomy tt on tr.term_taxonomy_id = tt.term_taxonomy_id
                                where p.post_type='businesslisting' and p.post_status='publish' 
                                        and pm.meta_key='listing_type' and tt.term_id = ".absint($_GET['catid'])."
                                order by pm.meta_value desc, p.post_date desc";
                        
                        $db_posts = $wpdb->get_results($sql);
                        
                        $total_rec = count($db_posts);
                        $from_page = $per_page*($paged-1) + 1;
                        $to_page = $per_page*($paged-1) + $per_page;
                        if($to_page > $total_rec) $to_page = $total_rec;
                        
                        $total_page = (int)$total_rec/$per_page;
                        if( $total_page*$per_page < $total_rec ) $total_page++;
                    ?>
                    <div class="busines-content">
                        <div class="pager">
                            <div class="display">Displaying <?php print $from_page;?> - <?php print $to_page;?> of <?php print $total_rec;?></div>
                             <?php if($total_page > 1) { ?>
                            <div class="paging">Go to: 1 2 3 4 5 6 ...</div>
                            <?php } ?>
                        </div>
                        <?php
                            foreach($db_posts as $post){      
                                update_post_meta($post->ID, 'business_count', get_post_meta($post->ID, 'business_count', TRUE) + 1);
                               
                                $website = strtolower(get_post_meta($post->ID, 'field_website', TRUE));
                                if(substr($website,0,7)!='http://') $website = 'http://'.$website;
                        ?>
                        <div class="content">
                            <div class="content-left">                                
                                <div class="description">
                                <a class="title" href="<?php print get_permalink( $post->ID ); ?>">
                                    <?php print $post->post_title; ?>
                                </a>
                                <?php print $post->post_content; ?>
                                <a title="read more" href="<?php print get_permalink( $post->ID ); ?>">...</a>
                                </div>                                
                                <div class="description">
                                    <strong><?php //by cat
                                            $term = get_term( get_post_meta($post->ID, 'field_hotelregion', TRUE),'hotelregion');  
                                            print $term->name;?></strong><br />
                                    <?php print get_post_meta($post->ID, 'field_address', TRUE).
                                                ', '.get_post_meta($post->ID, 'field_city', TRUE).', '.get_post_meta($post->ID, 'field_zip', TRUE);?><br />
                                    <?php print get_post_meta($post->ID, 'field_phone', TRUE);?> <br />
                                    <a href="<?php print get_permalink( $post->ID ); ?>">More Info</a> | <a target="_blank" href="<?php print $website;?>">Website</a> | <a href="<?php print get_permalink( $post->ID ); ?>#viewmap">Map</a><br /> 
                                </div>
                            </div>
                            <div class="thumbnail">
                                 <strong>$$$</strong>
                                <br class="clear">
                                <?php                                
                                if(get_post_meta($post->ID, 'field_photo', TRUE)) { ?>
                                <img height="120" width="120" alt="<?php print $post->post_title; ?>" src="/<?php print get_post_meta($post->ID, 'field_photo', TRUE);?>">
                                <?php } ?>
                            </div>
                        </div>
                        <?php                             
                            }   
                        // Reset Query
                        wp_reset_query();    
                        ?>                      
                        <div class="pager">
                            <div class="display">Displaying <?php print $from_page;?> - <?php print $to_page;?> of <?php print $total_rec;?></div>
                             <?php if($total_page > 1) { ?>
                            <div class="paging">Go to: 1 2 3 4 5 6 ...</div>
                            <?php } ?>
                        </div>
                    </div>
                </div><!--/.centerpage-->

			</div><!-- #content -->

		</div><!-- #primary -->



<?php get_footer(); ?>